﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;

namespace Maticsoft.Web.wap
{
    public partial class UserAccontEdit : BasePage
    {
        string _txt = string.Empty;
        string _val = string.Empty;
        BLL.Members meBLL = new BLL.Members();
        Model.Members meModel = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _txt = Request.QueryString["act"];
                InitData();
            }
        }

        protected void InitData()
        {
            meModel=meBLL.GetModel(DigitHelper.GetInt32(UserId));
            if (_txt == "name")
            {
                txt.InnerText = "姓名";
                val.Value = meModel.RealName;
            }
            if (_txt == "birthday")
            {
                txt.InnerText = "出生日期";
                val.Value = meModel.Birthday;
            }
            if (_txt == "sex")
            {
                txt.InnerText = "性别";
                val.Value = meModel.Sex=="1" ? "男" : "女";
            }
            if (_txt == "email")
            {
                txt.InnerText = "邮箱";
                val.Value = meModel.Email;
            }
            if (_txt == "qq")
            {
                txt.InnerText = "QQ";
                val.Value = meModel.QQ;
            }
            if (_txt == "weixin")
            {
                txt.InnerText = "微信";
                val.Value = meModel.Weixin;
            }
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            string errMsg = string.Empty;
            meModel = meBLL.GetModel(DigitHelper.GetInt32(UserId));
            if (_txt == "name")
            {
                meModel.RealName = val.Value;
            }
            if (_txt == "birthday")
            {
                meModel.Birthday = val.Value;
                DateTime dt = DateTime.Now;
                if (!DateTime.TryParse(val.Value, out dt))
                {
                    errMsg = "日期格式错误";
                }
            }
            if (_txt == "sex")
            {
                meModel.Sex = val.Value == "男" ? "1" : "0";
                if (val.Value != "男" && val.Value != "女")
                {
                    errMsg = "性别错误";
                }
            }
            if (_txt == "email")
            {
                meModel.Email = val.Value;
                if (val.Value.IndexOf("@") == -1)
                {
                    errMsg = "邮箱格式错误";
                }
            }
            if (_txt == "qq")
            {
                meModel.QQ = val.Value;
                Int64 qq = 0;
                if (!Int64.TryParse(val.Value, out qq))
                {
                    errMsg = "QQ格式错误";
                }
            }
            if (_txt == "weixin")
            {
                meModel.Weixin = val.Value;
            }
            if (string.IsNullOrEmpty(errMsg))
            {
                meBLL.Update(meModel);

                Response.Redirect("UserAccount.aspx");
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(Page.GetType(), Guid.NewGuid().ToString(), "alert('" + errMsg + "')", true);
            }
        }
    }
}